package com.friend.joey.applicationfriend.Adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.friend.joey.applicationfriend.R;
import com.friend.joey.applicationfriend.Classes.RememRecord;

import java.util.List;

//import static com.baidu.location.h.j.R;
//import static com.baidu.location.h.j.p;


/**
 * Created by Joey on 2017/4/25.
 */

public class RememAdapter extends RecyclerView.Adapter<RememAdapter.ViewHolder> {

    private List<RememRecord> RemList;
    private onRemCallback mCallback;

    public interface onRemCallback {
        void onDeleteclick(int itempostion);
        void onShowclick();
    }

    public void setOnRemCallbackListener(onRemCallback listener) {
        this.mCallback = listener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView addtime;
        TextView alerttime;
        TextView content;
        CheckBox setalert;
;        Button btn_del;

        public ViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.txt_btitle);
            addtime = (TextView) view.findViewById(R.id.txt_baddtime);
            alerttime = (TextView) view.findViewById(R.id.txt_balerttime);
            content = (TextView) view.findViewById(R.id.txt_bcontent);
            setalert = (CheckBox) view.findViewById(R.id.balert);
            btn_del = (Button) view.findViewById(R.id.button_bdel);
        }
    }

    public RememAdapter(List<RememRecord> remList) {
        RemList = remList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ffmem_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //String text=v.getContext().
                int pos=holder.getOldPosition();
                //Toast.makeText(v.getContext(), v.getTag().toString(), Toast.LENGTH_SHORT).show();
                if (mCallback != null) {
                    mCallback.onDeleteclick((Integer) v.getTag());
                }
            }
        });
        return new ViewHolder(view);
    }

    @Override

    public void onBindViewHolder(ViewHolder holder, int position) {
        RememRecord msg = RemList.get(position);
        //holder.getposition = position;
        holder.btn_del.setTag(position);
        String title=msg.getTitle();
        if(title=="" || title==null || title==" "){
            title="无标题";
        }
        holder.title.setText(title);
        holder.addtime.setText(msg.getAddtime());
        holder.content.setText(msg.getContent());
        if (msg.getType() == RememRecord.SET_ALERT){
            holder.setalert.setChecked(true);
            holder.alerttime.setText(msg.getAlerttime());
        } else if (msg.getType() == RememRecord.NOT_ALERT){
            holder.setalert.setChecked(false);
            holder.alerttime.setText("");
        }
    }

    @Override
    public int getItemCount() {
        return RemList.size();
    }
}
